package com.xhwl.logistics.service.impl.http;

import com.google.common.base.Strings;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.enums.VehicleParkingLotHandlerEnum;
import com.xhwl.logistics.service.cent.IDeviceService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import retrofit2.Call;
import retrofit2.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.xhwl.logistics.component.SignForCoSon;
import com.xhwl.logistics.dto.http.*;
import com.xhwl.logistics.dto.http.query.QueryCommonCoSonDTO;
import com.xhwl.logistics.service.http.ICoSonHttpService;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import javax.annotation.PostConstruct;


@Slf4j
//@Service
public class CoSonHttpServiceImpl {
    @Autowired
    private SignForCoSon signForCoSon;
    @Autowired
    private IDeviceService iDeviceService;

    private final Map<String, ICoSonHttpService> map = new HashMap<>();
    @Autowired
    private QueryDeviceService queryDeviceService;


    private ICoSonHttpService getCoSonHttpService(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        return map.get(vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost());
    }

    private <T> T coSonResponseHandle(Response<ResponseCoSonDTO<T>> response, String exceptionMessage) {
        if (response.code()!=200) {
            throw new BusinessException("请求科松查询接口返回的http状态码不为200");
        }
        ResponseCoSonDTO<T> body = response.body();
        if (Objects.isNull(body)) {
            throw new BusinessException("请求科松查询接口返回的响应消息体为空");
        }
        if (!"1".equals(body.getCode())) {
            if (Strings.isNullOrEmpty(exceptionMessage)) {
                throw new BusinessException("请求科松查询接口返回的业务码（Code）不为1，业务异常");
            } else {
                throw new BusinessException(exceptionMessage);
            }
        }
        return body.getData();
    }

    public CarportCoSonDTO queryCarport(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryCommonCoSonDTO queryCommonCoSonDTO) {
        String version = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getVersion();
        String appId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppId();
        String parkId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId();
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        signForCoSon.httpSign(queryCommonCoSonDTO,appId,parkId,version,vehicleParkingInfoBO);
        Call<ResponseCoSonDTO<CarportCoSonDTO>> call = getCoSonHttpService(vehicleIntegratedConfigBO).getSpace(queryCommonCoSonDTO);
        Response<ResponseCoSonDTO<CarportCoSonDTO>> response;
        try {
            response = call.execute();
        } catch (IOException e) {
            throw new BusinessException("执行请求科松查询车位信息接口出现IO异常");
        }

        return coSonResponseHandle(response, "请求科松查询车位信息接口业务异常");
    }

    public ParkingLotInfoCoSonDTO queryParkingLotInfo(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryCommonCoSonDTO queryCommonCoSonDTO){
        String version = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getVersion();
        String appId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppId();
        String parkId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId();
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        signForCoSon.httpSign(queryCommonCoSonDTO,appId,parkId,version,vehicleParkingInfoBO);
        Call<ResponseCoSonDTO<ParkingLotInfoCoSonDTO>> call = getCoSonHttpService(vehicleIntegratedConfigBO).getParkInfo(queryCommonCoSonDTO);
        Response<ResponseCoSonDTO<ParkingLotInfoCoSonDTO>> response;
        try {
            response = call.execute();
        } catch (IOException e) {
            throw new BusinessException("执行请求科松查询车场信息接口出现IO异常");
        }
        return coSonResponseHandle(response, "请求科松查询车场信息接口业务异常");
    }

    public List<DeviceInfoCoSonDTO> queryDeviceInfo(VehicleIntegratedConfigBO vehicleIntegratedConfigBO,QueryCommonCoSonDTO queryCommonCoSonDTO) {
        String version = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getVersion();
        String appId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getAppId();
        String parkId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId();
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        signForCoSon.httpSign(queryCommonCoSonDTO,appId,parkId,version,vehicleParkingInfoBO);
        Call<ResponseCoSonDTO<List<DeviceInfoCoSonDTO>>> call = getCoSonHttpService(vehicleIntegratedConfigBO).getDeviceInfo(queryCommonCoSonDTO);
        Response<ResponseCoSonDTO<List<DeviceInfoCoSonDTO>>> response;
        try{
            response = call.execute();
        } catch(IOException e){
            throw new BusinessException("执行请求科松查询设备信息接口出现IO异常");
        }
        return coSonResponseHandle(response, "请求科松查询设备信息接口业务异常");
    }

    @PostConstruct
    public void init() {
        List<VehicleIntegratedConfigBO> vehicleIntegratedConfigBOS = queryDeviceService.queryIntegratedConfigs();
        vehicleIntegratedConfigBOS.stream()
                .filter(vehicleIntegratedConfigBO -> VehicleParkingLotHandlerEnum.VEHICLE_PARKINGLOT_COSON.getProductId().equals(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId()))
                .map(vehicleIntegratedConfigBO -> vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getHost())
                .filter(host -> !Strings.isNullOrEmpty(host))
                .distinct()
                .forEach(host -> {
                    Retrofit retrofit = new Retrofit.Builder().baseUrl(host + "/").addConverterFactory(GsonConverterFactory.create()).build();
                    ICoSonHttpService coSonHttpService = retrofit.create(ICoSonHttpService.class);
                    map.put(host, coSonHttpService);
                });
    }

}

